// 路由配置
import { createRouter, createMemoryHistory } from 'vue-router';
import { userAuthStore } from '../store/authStore';
var routes = [
	// 重定向根路径到登录页面
	{
		path: '/',
		redirect: '/login'
	},
	{
		component: () => import("@/view/Login.vue"),
		name: 'Login',
		path: "/login"
	},
	{
		component: () => import("@/view/homePageLayout/Index.vue"),//导入组件
		name: 'Index',
		path: "/index",
		children: [
			{
				component: () => import("@/view/purchase/Medicine.vue"),
				name: 'Medicine',
				path: '/lhhMedicine/getMedicineList',
				meta: { title: '药品信息列表', showTab: true }
			},
			{
				component: () => import("@/view/purchase/Supplier.vue"),
				name: 'Supplier',
				path: '/lhhSupplier/getSupplier',
				meta: { title: '供应商列表', showTab: true }
			}
		]
	}
];

const router = createRouter({
	history: createMemoryHistory(),
	//命名路由的使用
	routes,
})

//实时监控路由的变化
router.beforeEach((to, from, next) => {
	const userStore = userAuthStore();
	if (to.meta.requiresAuth && !userStore.token) {
		next('/login'); //未登录跳转登录页
	} else if (to.path === '/login' && userStore.token) {
		next('/index') // 已登录用户访问登录页，跳转首页
	} else {
		next(); //放行
	}
});

export default router;